
/* SOURCE-CODE-START */

"use strict";

var globalConfig = (function() {
if (typeof globalConfig !== 'undefined') {
  return globalConfig;
}



if (typeof jQuery === 'undefined') {
  throw new Error('jquery.js required');
} else if (typeof jQuery.cookie === 'undefined') {
  throw new Error('jquery.cookie.js required');
}




var globalConfig = {};

// 系统租户ID
globalConfig.SYSTEM_TENANT_ID = '0';
// 当前语言代码
globalConfig.LANGUAGE_CODE = '${currentLang()}';

globalConfig.dataTypeAttributeName = 'data-x-type';

globalConfig.csrfTokenCookieName = 'csrf-token';

globalConfig.csrfTokenHeaderName = 'x-csrf-token';

globalConfig.csrfTokenStorageKey = 'csrf-token';




// 处理 CSRF 令牌
solveCsrfToken();

/**
 * 处理 CSRF 令牌
 * @returns {string}
 */
function solveCsrfToken() {
  var csrfToken = jQuery.cookie(globalConfig.csrfTokenCookieName);

  if (globalUtils.isNotEmptyString(csrfToken)) {
    localStorage.setItem(globalConfig.csrfTokenStorageKey, csrfToken);
    jQuery.removeCookie(globalConfig.csrfTokenCookieName);
  } else {
    csrfToken = localStorage.getItem(globalConfig.csrfTokenStorageKey);
  }

  return csrfToken;
}

//
// 自定义 jquery.js 配置
//
if (typeof jQuery != 'undefined') {
  jQuery.ajaxSetup({
    beforeSend: function (request) {
      var csrfToken = solveCsrfToken();

      // 添加 CSRF 令牌到请求头
      if (globalUtils.isNotEmptyString(csrfToken)) {
        request.setRequestHeader(globalConfig.csrfTokenHeaderName, csrfToken);
      }
    },
    complete: function (jqXHR, textStatus) {
      solveCsrfToken();
    }
  });

  if (typeof jQuery.validator != 'undefined') {
    jQuery.validator.addMethod('customCode', function (value, element, params) {
      return this.optional(element) || /^\w*$/.test(value);
    }, '${t("ui.system.common.validation.customCode")}');
  }
}

//
// 自定义 ui-data.js 配置
//
if (typeof uiData !== 'undefined') {
  uiData.Model.config.defaultNameAttributeName = 'data-x-name';
  uiData.Model.config.defaultTypeAttributeName = globalConfig.dataTypeAttributeName;
}

//
// 自定义 pure-frontend.js 配置
//
if (typeof Pure !== 'undefined') {
  // 单页面应用的基础URL
  Pure.config.singlePageBaseUrl = '/';

  Pure.config.uiNameAttributeName = 'data-x-ui';

  // 设置 AJAX 错误的默认处理逻辑
  jQuery.ajaxSetup({
    error: function (jqXHR, textStatus, errorThrown) {
      Pure.fn.AjaxResult.handleAjaxError(null, jqXHR, textStatus, errorThrown);
    }
  });

  var jqTitle = jQuery('title');
  var titleText = jqTitle.text();

  // 切换视图时修改浏览器标题
  Pure.fn.BrowserTitle.setBrowserTitle = function (viewTitle) {
    var newTitleText = globalUtils.isNotEmptyString(viewTitle)
      ? (titleText + ' - ' + viewTitle) : titleText;

    jqTitle.text(newTitleText);
  };

  Pure.fn.ViewLoader.prototype.preRenderView = function (url, data, textStatus, jqXHR) {
    var viewResponse = new Pure.fn.ViewResponse(url, jqXHR);
    var viewInfo = viewResponse.getViewInfo();
    var viewName = viewInfo.getViewName();

    if (viewName === 'login') {
      Pure.app.showError('${t("ui.system.common.msg.unauthorizedOrTimeout")}');
      return false;
    }

    return true;
  };

  Pure.fn.AjaxCallService.prototype.convertData = function (data, opts) {
    data = globalUtils.emptyObjectIfNullOrUndefined(data);
    opts = globalUtils.emptyObjectIfNullOrUndefined(opts);

    if (!(opts.convertEmptyStringToNull === false)) {
      // 空字符串转换成 null 值
      data = globalUtils.convertEmptyStringToNull(data);
    }

    return JSON.stringify(data);
  };
}



return globalConfig;
})();

/* SOURCE-CODE-END */

export { globalConfig };
